package tankgame03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

//为了监听键盘事件，实现KeyListener
//为了让panel不停的重绘子弹，需要将panel实现Runnable结果
public class MyPanel extends JPanel implements KeyListener,Runnable {
    //坦克大战区域
    Hero hero=null;
    //定义敌人坦克
    Vector<EnemyTank> enemyTanks=new Vector<>();
    int enemyTankSize=3;
    public MyPanel(){
        hero =new Hero(100,100);//初始化自己的坦克
        //初始化敌人的坦克
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1),0);
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);//默认是黑色
        //画坦克-封装方法
        drawTank(hero.getX(),hero.getY(),g,hero.getDirection(),1);

        //画出敌人的坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            //取出坦克
            EnemyTank enemyTank=enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(),g,enemyTank.getDirection(),0);

        }
        //画出hero的子弹
        if (hero.shot!=null&&hero.shot.isLive==true){
            g.fill3DRect(hero.shot.x,hero.shot.y,5,5,false);
        }
    }

    //画坦克方法
    //x,y是左上角坐标
    //g是画笔
    //direction是方向
    //type是类型
    public void drawTank(int x,int y,Graphics g,int direction,int type){
        switch (type){//设置不同类型坦克的颜色
            case 0://敌人的坦克
                g.setColor(Color.CYAN);
                break;
            case 1://我们的坦克
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        //direction 表示方向，0上 1右 2下 3左
        switch (direction){
            case 0:
                //上
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y,x+20,y+30);
                break;
            case 1:
                //右
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x+60,y+20);
                break;
            case 2:
                //下
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x+30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+60,x+20,y+30);
                break;
            case 3:
                //左
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x,y+20);
                break;
            default:
                System.out.println("没处理");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    //处理wasd键按下的情况
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode()==KeyEvent.VK_W){
            //改变坦克方向
            hero.setDirection(0);
            hero.moveUp();
        }else if (e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirection(3);
            hero.moveRight();
        }else if (e.getKeyCode()==KeyEvent.VK_S){
            hero.setDirection(2);
            hero.moveDown();
        }else if (e.getKeyCode()==KeyEvent.VK_D){
            hero.setDirection(1);
            hero.moveLeft();
        }
        //如果用户按下J，就发射子弹
        if (e.getKeyCode()==KeyEvent.VK_J){
            hero.shotEnemy();
        }
        this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {//每隔100毫秒重绘当前区域
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.repaint();
        }
    }
}
